use crate::cli::{Cli, VersionCliArgs, VersionSubCommands};
use crate::context::CONTEXT;
use crate::model::version::VersionItem;
use crate::{cli, tui, utils};
use clap::{Command, CommandFactory};

use crate::plugin::Plugin;
use std::path::PathBuf;

pub fn run(args: VersionCliArgs) {
    match args.command {
        Some(VersionSubCommands::Use {
            name,
            version,
            global,
            project,
            session,
        }) => {
            println!(
                "Use {} {} {} {} {}",
                name, version, global, project, session
            );
            // let map_version: HashMap<String, Vec<VersionItem>> = HashMap::new();
            if version == cli::USE_VERSION_DEFAULT {
                let list_version =
                    utils::list_subdir(CONTEXT.version_dir.join(PathBuf::from(name.clone())));
                if list_version.len() == 0 {
                    println!("{} version not found", name);
                } else {
                    println!("version list:{}", list_version.join(","));
                }
            }
        }
        Some(VersionSubCommands::Remove { name, version }) => {
            println!("Remove {}{}", name, version);
        }
        Some(VersionSubCommands::Add { name, version }) => {
            println!("Add {}{}", name, version);
        }
        Some(VersionSubCommands::Search { name }) => {
            println!("Search{}", name);
            let plugin = Plugin::new("jdk".to_string(), None);
            let list_version = plugin.versions();
            println!("{:?}", list_version);
            // let result = tui::search().expect("tui search error");
            // println!("{:?}", result);
        }
        None => {
            let help = Command::get_matches_from(Cli::command(), vec!["sdk --help"]);
            println!("{:?}", help)
        }
        Some(VersionSubCommands::Plugin { .. }) => {
            panic!("inner not support");
        }
    }
}
